import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../providers/provider_container.dart';
import '../../video_tag/entity/video_bean.dart';
import '../intent/video_list_state.dart';
import '../model/video_list_viewmodel.dart';

class VideoListPage extends ConsumerStatefulWidget {
  final String json;

  VideoListPage({required this.json});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() {
    return _VideoListPageState();
  }
}

class _VideoListPageState extends ConsumerState<VideoListPage> {
  VideoListViewModel get viewModel => ref.read(videoListViewModelProvider.notifier);

  @override
  void initState() {
    super.initState();
    // 在页面初始化时执行网络请求
    WidgetsBinding.instance.addPostFrameCallback((_) {
      viewModel.initData(widget.json);
    });
  }

  @override
  Widget build(BuildContext context) {
    final VideoListState state = ref.watch(videoListViewModelProvider);
    return Scaffold(
      appBar: AppBar(title: Text('标签列表'), centerTitle: true),
      body: SafeArea(
        child: Container(
          width: double.infinity,
          height: double.infinity,
          child: ListView.builder(
            itemCount: state.uiList.length,
            itemBuilder: (context, index) {
              VideoBean item = state.uiList[index];
              return InkWell(
                onTap: () {
                  viewModel.goNextPage(item.name, item.vUrl);
                },
                child: Container(
                  child: Column(
                    children: [
                      SizedBox(height: 10),
                      Row(
                        children: [
                          SizedBox(width: 12),
                          Image.network(
                            item.pUrl,
                            width: 100,
                            height: 100,
                            errorBuilder: (context, error, stackTrace) {
                              return Image.asset('assets/images/img_error.jpg', width: 100, height: 100, fit: BoxFit.cover);
                            },
                          ),
                          SizedBox(width: 12),
                          Expanded(child: Text(item.name)),
                          SizedBox(width: 12),
                        ],
                      ),
                      SizedBox(height: 10),
                    ],
                  ),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}
